﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Web.SessionState;


namespace SES.CMS
{
    public partial class SanPhamChiTiet : BasePage
    {
        ShopingCart vpp = new ShopingCart();
        public void Page_Load(object sender, EventArgs e)
        {
            InitForm();
        }
        public void InitForm()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int SanPhamID = int.Parse(Request.QueryString["ID"].ToString());
                DataTable dtSanPham = new cmsSanPhamBL().SelectBySanPhamID(SanPhamID);
                if (dtSanPham.Rows.Count > 0)
                {
                    lblProductID.Text = dtSanPham.Rows[0]["MaSanPham"].ToString();
                    lblSaleOff.Text = dtSanPham.Rows[0]["GiamGia"] + "%";

                    if (Session["Lang"].ToString() == "VN")
                    {
                        lblProductName.Text = dtSanPham.Rows[0]["TenSanPham"].ToString();
                        lblDescription.Text = dtSanPham.Rows[0]["Mota"].ToString();
                        lblPrice.Text = string.Format("{0:0,0}", dtSanPham.Rows[0]["DonGia"]) + "đ";
                        lblDetailDescription.Text = dtSanPham.Rows[0]["MoTaChiTiet"].ToString();
                        Page.Title = dtSanPham.Rows[0]["TenSanPham"].ToString();
                    }
                    else if (Session["Lang"].ToString() == "EN")
                    {
                        lblProductName.Text = dtSanPham.Rows[0]["ProductName"].ToString();
                        lblDescription.Text = dtSanPham.Rows[0]["Description"].ToString();
                        lblPrice.Text = string.Format("{0:0,0}", dtSanPham.Rows[0]["Price"]) + "$";
                        lblDetailDescription.Text = dtSanPham.Rows[0]["DetailDescription"].ToString();
                        Page.Title = dtSanPham.Rows[0]["ProductName"].ToString();

                    }
                   
                }
            }
        }

        protected void hplAddToCart_Click(object sender, EventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            int soLuong = 1;
            try
            {
                if (txtQuantity.Text != "" && (int.Parse(txtQuantity.Text) > 0 && int.Parse(txtQuantity.Text) < 200))
                {
                    soLuong = int.Parse(txtQuantity.Text);
                    HttpSessionState ss = HttpContext.Current.Session;
                    string session = ss.SessionID;
                    Int64 id = Int64.Parse(Request.QueryString["ID"].ToString());
                    cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });

                    float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                    float price = (float)((float)obj1.Price - ((float)obj1.Price * obj1.GiamGia) / 100);

                    long ClientLoginID = 0;
                    if (Session["IDDN"] != null)
                        ClientLoginID = long.Parse(Session["IDDN"].ToString());
                    DataTable tb = new DataTable();
                    if (Session["GioHang"] != null)
                    {
                        vpp.setTable((DataTable)Session["GioHang"]);
                        tb = vpp.getTable();
                        bool kt = false;
                        foreach (DataRow dr in tb.Rows)
                        {
                            if (Int64.Parse(dr[1].ToString()) == id)
                                kt = true;
                        }
                        if (kt == true)
                            tb = vpp.upDate(tb, id, soLuong, gia, price);
                        else
                            tb = vpp.addSP(id, soLuong, (gia * soLuong), (price * soLuong));
                        Session["GioHang"] = tb;
                    }
                    else
                    {
                        tb = vpp.init(id, soLuong, (gia * soLuong), (price * soLuong));
                        Session["GioHang"] = tb;
                    }

                    Ultility.Alert("Đặt mua thành công!", url);
                }
            }
            catch
            {
                Ultility.Alert("Lỗi!", url);
            }
 
        }
    }

}
